//new
/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var userRelation = require('./routes/userRelation');
var friendShow = require('./routes/friendShow');
var message = require('./routes/message/message.js');
var group = require('./routes/group/group.js');
var merchant = require('./routes/merchant/merchant.js');
var classList = require('./routes/classList/index.js');

var http = require('http');
var path = require('path');


//key = '360hrA#-aK@b9T.Jzd,O24!52';
key = '360hrA#-aK@b9T.Jzd,O24!52';
crypto = require('crypto');
myDb = {user: "root" , password : "abc123" , DbName:"weibang"};
groupName = ["美食","购物","趣玩","休闲娱乐","人物","行业","旅游","影视","品牌","服务"];
aes = {
	'encode' : function(data, secretKey){
		var cipher = crypto.createCipher('aes-128-cbc',secretKey);  
		return cipher.update(data,'utf8','base64') + cipher.final('base64');  
	},
	'decode' : function(data, secretKey){
		var cipher = crypto.createDecipher('aes-128-cbc',secretKey);
		var code = cipher.update(data,'base64','utf8') + cipher.final('utf8');
			if(code)
				return code;
			else
				return "";
	}
}


//按对象某个值排序
//b2.sort(bojby('addtime'));
bojby = function(name){
	return function(o,p){
		var a, b;
        if(typeof o === "object" && typeof p === "object" && o && p){
            a = o[name];b = p[name];
			if(a === b)
                return 0;
            if(typeof a === typeof b)
                return a < b ? -1 : 1;
            return typeof a < typeof b ? -1 : 1;
        }
        else
			throw ("error");
    }
}




var app = express();

// all environments
app.set('port', process.env.PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
//app.use(express.bodyParser());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')){
	app.use(express.errorHandler());
}



app.get('/', routes.index);
app.get('/cs', routes.cs);
app.get('/mongodb', routes.mongodb);
app.get('/sql', routes.sql);
app.get('/css', routes.css);
app.get('/cssc', routes.cssc);
app.get('/socket', routes.socket);
app.post('/socket', routes.socket);
app.post('/homeData', routes.homeData);


//首页

//分类
app.post('/classList/index', classList.index);
app.post('/classList/index', classList.index);



//用户模块
app.post('/user/register', user.register);
app.post('/user/login', user.login);
app.post('/user/dataEdit', user.dataEdit);
app.post('/user/headUp', user.headUp);
app.get('/user/headUp', user.headUp);
app.get('/aes', routes.aes);


//商户模块
app.post('/merchant/add', merchant.add);
app.post('/merchant/edit', merchant.edit);
app.post('/merchant/view', merchant.view);
app.post('/merchant/list', merchant.list);
app.get('/merchant/sh', merchant.sh);
app.post('/merchant/sh', merchant.sh);




//好友交互
app.post('/userRelation/requestFriends', userRelation.requestFriends);
app.post('/userRelation/acceptFriend', userRelation.acceptFriend);
app.post('/userRelation/friendData', userRelation.friendData);
//app.get('/userRelation/friendData', userRelation.friendData);
app.post('/userRelation/friendList', userRelation.friendList);


//正在晒
app.post('/friendShow/ShowAdd', friendShow.ShowAdd);
//app.get('/friendShow/ShowAdd', friendShow.ShowAdd);
app.post('/friendShow/ShowList', friendShow.ShowList);
app.post('/friendShow/ShowOpinion', friendShow.ShowOpinion);
app.post('/friendShow/ShowComment', friendShow.ShowComment);


//消息
app.post('/message/messageSum', message.messageSum);
app.post('/message/messageList', message.messageList);
app.post('/message/pointSend', message.pointSend);
app.post('/message/pointRead', message.pointRead);
app.post('/message/groupSend', message.groupSend);
app.post('/message/groupRead', message.groupRead);

//app.post('/message/messageSum', message.messageSum);
//app.post('/message/groupRead', message.groupRead);


//群组模块
app.post('/group/create', group.create);
app.get('/group/create', group.create);
app.post('/group/list', group.list);
app.post('/group/memberlist', group.memberlist);
app.post('/group/dataEdit', group.dataEdit);
app.post('/group/addVerify', group.addVerify);
app.post('/group/confirmVerify', group.confirmVerify);
app.post('/group/dataShow', group.dataShow);



http.createServer(app).listen(app.get('port'), function(){
	console.log('Express server listening on port ' + app.get('port'));
});
